/**
 * 
 */
package com.bitBusy.manageyourriches.alarmSystem;

import java.io.File;
import java.util.Calendar;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.bitBusy.manageyourriches.activity.AlarmActivity;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentReminder;
import com.bitBusy.manageyourriches.database.managers.DatabaseInteractor;
import com.bitBusy.manageyourriches.emailManagement.Postman;
import com.bitBusy.manageyourriches.emailManagement.Postman.EMAILSENTCODES;
import com.bitBusy.manageyourriches.utilityClasses.display.LocalNotificationManager;
import com.bitBusy.manageyourriches.utilityClasses.filehandlers.DataExportRequestors;
import com.bitBusy.manageyourriches.utilityClasses.filehandlers.ReportFileBuilder;

/**
 * @author SumaHarsha
 *
 */
public class AlarmReceiverService extends Service implements DataExportRequestors{

	 
	/**
	 * @param name
	 */
	public AlarmReceiverService() {
		super();
		// TODO Auto-generated constructor stub
	}

	private PaymentReminder myReminder;
	
	    @Override
	    public IBinder onBind(Intent arg0)
	    {
	       // TODO Auto-generated method stub
	        return null;
	    }
	 
	    @Override
	    public void onCreate()  			
	    {
	    	super.onCreate();
	    }
	    @Override
	    public void onDestroy()  			
	    {
	    	myReminder = null;
	    	super.onDestroy();
	    }
	    @Override
	    public int onStartCommand(Intent the_intent, int flags, int startId) {


	    if (the_intent != null && the_intent.getExtras() != null)
    	{
	    	myReminder = (PaymentReminder)the_intent.getExtras().getParcelable(AlarmActivity.ALARMDETAILS);
	    	if (myReminder != null)
	    	{
	    		if(myReminder.isRecurrent()== 0)
	    		{
	    			showNotification(myReminder.getNote());
	    	    	AlarmManagement.deleteAlarmFromDB(myReminder);

	    		}
	    		else
	    		{
		    		sendEmail();
	    		}
	    	}
    	}
	    return START_STICKY;
}
	     
	    /**
		 * 
		 */
		private void sendEmail() {
			ReportFileBuilder fileBuilder = new ReportFileBuilder(this);
    		fileBuilder.registerRequestors(this);
    		new DatabaseInteractor(this).getReportTillDate(fileBuilder);
			
		}

		/* (non-Javadoc)
		 * @see com.bitBusy.manageyourriches.utilityClasses.filehandlers.DataExportRequestors#acceptFile(java.io.File)
		 */
		/**
		 * 
		 */
		private void showNotification(String the_notification) {
			LocalNotificationManager.showNotification(this, the_notification);
  			//stopSelf();
		}

		@Override
		public void acceptFile(File the_file) {
			if (the_file != null)
			{
				sendEmail(the_file);
			}
		}
	      /**
		 * @param the_file
		 */
		private void sendEmail(File the_file) {
			Postman.EMAILSENTCODES postAckCode = Postman.send(myReminder.getEmail(), the_file);
			emailSentProcessing(postAckCode);				
		}

		/**
		 * @param postAckCode
		 */
		private void emailSentProcessing(EMAILSENTCODES the_postAckCode) {
			if(the_postAckCode.equals(Postman.EMAILSENTCODES.SUCCESS))
			{
				setNextReminder();
				AlarmManagement.deleteAlarm(myReminder);		
				showNotification(Postman.EMAILSENT);

			}
			else if(the_postAckCode.equals(Postman.EMAILSENTCODES.FAILUREEMAILADDRESS))
			{
				//DisplayMessages.displayUserError(this, Postman.EMAILERROR);
				showNotification(Postman.EMAILERROR);
			}
			else if(the_postAckCode.equals(Postman.EMAILSENTCODES.FAILURESYSEM))
			{
				//DisplayMessages.displayInternalError(this, Postman.SYSTEMFAIL);
				showNotification(Postman.SYSTEMFAIL);
			}
	
		}

		/**
		 * 
		 */
		private boolean setNextReminder() {
			PaymentReminder nextReminder = new PaymentReminder();
			setNextReminderDetails(nextReminder);
			return AlarmManagement.setAlarm(nextReminder);
		}

		/**
		 * @param nextReminder
		 */
		private void setNextReminderDetails(PaymentReminder nextReminder) {
			nextReminder.setEmail(myReminder.getEmail());
			nextReminder.setIsMonthly(myReminder.isMonthly());
			nextReminder.setIsRecurrent(myReminder.isRecurrent());
			nextReminder.setNote(myReminder.getNote());
			nextReminder.setHour(myReminder.getHour());
			nextReminder.setMinute(myReminder.getMinute());
			setDate(nextReminder);
		}

		/**
		 * @param nextReminder
		 */
		private void setDate(PaymentReminder the_nextReminder) {
			Calendar calendar = Calendar.getInstance();
			calendar.set(myReminder.getYear(), myReminder.getMonth(), myReminder.getDay());
	
			if (the_nextReminder.isMonthly() == 0)
			{
				calendar.add(Calendar.DAY_OF_MONTH, 7);			
			}
			else
			{
				calendar.add(Calendar.MONTH, 1);
			}
			the_nextReminder.setDay(calendar.get(Calendar.DAY_OF_MONTH));
			the_nextReminder.setMonth(calendar.get(Calendar.MONTH));
			the_nextReminder.setYear(calendar.get(Calendar.YEAR));
		}

			
	 
	}
